//
//  MKReaderParser.swift
//  Fanmugua
//
//  Created by MK on 2021/3/27.
//  Copyright © 2021 taolang. All rights reserved.
//

import UIKit

class MKReaderParser: NSObject {
    
    /// 内容分页
    /// - Parameters:
    ///   - attrString: 内容
    ///   - rect: 显示范围
    ///   - isFirstChapter: 是否为本文章第一个展示章节,如果是则加入书籍首页。
    /// - Returns: 内容分页列表
    @objc class func paging(attrString:NSAttributedString, rect:CGRect, isFirstChapter:Bool = false) -> [MKReaderPageModel] {

        var pageInfos:[MKReaderPageModel] = []
        let ranges = MKCoreText.getPagingRanges(attrString: attrString, rect: rect)

        if !ranges.isEmpty {
            let count = ranges.count
            for i in 0..<count {
                let range = ranges[i]
                let pageInfo = MKReaderPageModel()
                let content = attrString.attributedSubstring(from: range)
                
                if content.string != "\n" {
                    
                    pageInfo.range = range
                    pageInfo.content = content
                    pageInfo.page = NSNumber(value: i)
                    pageInfo.contentSize = rect.size
                    if i == count-1 {
                        pageInfo.contentSize = CGSize(width: rect.size.width,
                                                      height: MKCoreText.getAttrStringHeight(attrString: content, maxW: rect.size.width))
                        pageInfo.lastPage = true
                    }
                    pageInfos.append(pageInfo)
                }
                
            }
            
            
        }
        return pageInfos
    }

    /// 内容排版
    /// - Parameter content: 内容
    /// - Returns: 整理好的内容
    @objc class func contentTypeSetting(content:String) ->String {
        /// 替换单换行
        var content = content.replacingOccurrences(of: "\r", with: "")
        /// 替换换行 以及 多个换行 为 换行加空格
        content = content.replacingCharacters("\\s*\\n+\\s*", "\n" + MKREADER_PH_SPACE)
        return content
    }
//
//    /** 解码URL
//     * - Parameter url: 文件路径
//     * - Returns: 内容
//     */
//    @objc class func encode(url:URL) -> String {
//        var content = ""
//        if url.absoluteString.isEmpty {
//            return content
//        }
//        // utf8
//        content = encode(url: url, encoding: String.Encoding.utf8.rawValue)
//        // 进制编码
//        if content.isEmpty {
//            content = encode(url: url, encoding: 0x80000632)
//        }
//        if content.isEmpty {
//            content = encode(url: url, encoding: 0x80000631)
//        }
//        if content.isEmpty {
//            content = ""
//        }
//        return content
//    }
//
//    /** 解析URL
//     * - Parameters:
//     *   - url: 文件路径
//     *   - encoding: 进制编码
//     * - Returns: 内容
//     */
//    @objc class func encode(url:URL, encoding:UInt) ->String {
//        do{
//            return try NSString(contentsOf: url, encoding: encoding) as String
//        }catch{}
//        return ""
//    }
}
